package filecheck;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileCompare {
	private File file;
//	private FileInputStream fis;
	public FileCompare(){
		file = null;
	}
	public void doFileCompare(String f){
		file = new File(f);
		String filePath ="H:\\test\\src\\";
		String theOtherFilePath = "H:\\test\\src2\\";
		FileInputStream fis = null;
		try{
			byte []b = new byte[1000];
			int length = 0;
			StringBuffer sb = new StringBuffer();
			fis = new FileInputStream(file);
			length = fis.available();
			while(length>0){
				if(length>1000){
					length = 1000;
				}
				fis.read(b,0,length);
				for(int i = 0;i<length;i++){
					sb.append((char)b[i]);
				}		
				length = fis.available();
			}
			int start = 0,end = 0;
			int count = 0;
			while(start<sb.length()&&end<sb.length()){
				count++;
				if(count>1000){
					System.out.println("count break");
					break;
				}
				end = sb.indexOf("\r\n",start);
				String s = "";
				if(end == -1){
					end = sb.length();
				}
				s = sb.substring(start,end );
				String s2 = s.replace(filePath, theOtherFilePath);
				boolean judge = compareTwoFile(s,s2);
				if(judge==false){
					System.out.println(s+" file is different");
					
				}else{
					System.out.println(" ok");
				}
				start = end+2;
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		finally{
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	private boolean compareTwoFile(String file1,String file2){
		File f1= new File(file1);
		File f2= new File(file2);
		FileInputStream fis1 = null;
		FileInputStream fis2 = null;
		boolean judge = true;
		try{
			fis1 = new FileInputStream(file1);
			fis2 = new FileInputStream(file2);
			byte []b1= new byte[1000];
			byte []b2= new byte[1000];
			int length = fis1.available();
			if(length != fis2.available()){
				judge = false;
			}
			int count = 0;
			while(length>0&&judge == true){
				count ++;
				if(count>1000){
					System.out.println("count break");
					break;
				}
				if(length >1000){
					length =1000;
				}
				fis1.read(b1,0,length);
				fis2.read(b2,0,length);
				for(int i=0;i<length;i++){
					if(b1[i]!=b2[i]){
						judge = false;
					}
				}
				length = fis1.available();
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
				fis1.close();
				fis2.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		return judge;
	}

}
